package com.linkrenhe.frame.http;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.os.Handler;

public abstract class HttpRequest {
	public static final String REQUEST_METHOD_GET = "GET";
	public static final String REQUEST_METHOD_POST = "POST";
	public static final int FIRST_LEVEL_TOP = 1;
	public static final int FIRST_LEVEL_BOTTOM = 2;
	public static final int FIRST_LEVEL_SOON = 3;
	public static final int FIRST_LEVEL_CLEAR_TOP = 4;
	public static final int REQUEST_TYPE_ACTION = 1;
	public static final int REQUEST_TYPE_DOWNLOAD = 2;
	public static final int REQUEST_TYPE_UPLOAD = 3;
	protected String mUrl;
	protected int mFirstLevel = FIRST_LEVEL_TOP;
	private boolean isResponse = false;
	private boolean isExecute = false;
	private Handler mHandler ;
	private Object[] mObArrary;
	protected String mRequestMethod;
	private HashMap<String, Object> mHeaderMap;
	protected HttpSimpleListener mHttpListener = null;
	private List<HttpRequest> mSameRequestList = new ArrayList<HttpRequest>();
	private Object mPostURLParam;
	/**
	 * 
	 * @param url
	 * @param paramMap
	 */
	public HttpRequest(String url,String requestMethod){
		this.mUrl = url;
		this.mHandler = null;
		this.mRequestMethod = requestMethod;
		this.mHeaderMap = null;
	}
	
	/**
	 * 
	 * @param url
	 * @param paramMap
	 * @param handler
	 */
	public HttpRequest(String url,Handler handler,String requestMethod){
		this.mUrl = url;
		this.mHandler = handler;
		this.mRequestMethod = requestMethod;
		this.mHeaderMap = null;
	}
	
	public HttpRequest(String url,Handler handler,String requestMethod,HashMap<String, Object> headerMap){
		this.mUrl = url;
		this.mHandler = handler;
		this.mRequestMethod = requestMethod;
		this.mHeaderMap = headerMap;
	}
	
	/**
	 * 
	 * @param url
	 * @param paramMap
	 * @param firstLevel
	 * @param handler
	 */
	public HttpRequest(String url,int firstLevel,Handler handler,String requestMethod,HashMap<String, Object> headerMap){
		this.mUrl = url;
		this.mFirstLevel = firstLevel;
		this.mHandler = handler;
		this.mRequestMethod = requestMethod;
		this.mHeaderMap = headerMap;
	}
	
	public void setUrl(String mUrl) {
		this.mUrl = mUrl;
	}

	public void setHeaderMap(HashMap<String, Object> mHeaderMap) {
		this.mHeaderMap = mHeaderMap;
	}
	
	public HashMap<String, Object> getHeaderMap() {
		return mHeaderMap;
	}

	public void setData(Object... ob){
		this.mObArrary = ob;
	}
	public Object getData(){
		if(mObArrary!=null){
			if(mObArrary.length==1){
				return mObArrary[0];
			}else{
				return mObArrary;
			}
		}else{
			return null;
		}
	}
	
	public boolean isResponse() {
		return isResponse;
	}

	public void setResponse(boolean isResponse) {
		this.isResponse = isResponse;
	}

	public boolean isExecute() {
		return isExecute;
	}

	public void setExecute(boolean isExecute) {
		this.isExecute = isExecute;
	}

	public String getUrl() {
		return mUrl;
	}

	public Handler getHandler() {
		return mHandler;
	}
	
	public HttpSimpleListener getHttpListener() {
		return mHttpListener;
	}
	public void setPostUrlParamData(String paramData){
		this.mPostURLParam = paramData;
	}
	public void setPostUrlParamData(HashMap<String, String> paramData){
		this.mPostURLParam = paramData;
	}
	public Object getPostUrlParamData(){
		return this.mPostURLParam;
	}
	public boolean isComplete(){
		if(HttpHelper.hasProtocol(mUrl)&&
			(mRequestMethod == REQUEST_METHOD_GET || mRequestMethod == REQUEST_METHOD_POST)){
			return true;
		}
		return false;
	}
	public abstract int getFirstLevel();
	public abstract long getReadTimeout();
	
	public abstract long getConnectTimeout();
	public abstract Object getParamData() throws Exception;
	public abstract HttpResponse onPreExecute() throws Exception;
	
	public abstract void onPreExecute(HttpURLConnection connection) throws Exception;
	
	public abstract HttpResponse onDoingExecute(HttpURLConnection connection) throws Exception;
	public abstract void onPostExecute(boolean isSuccess);
	public abstract HttpResponse createErrorResponse(int state,HttpRequest httpTask);
	public void setHttpListener(HttpSimpleListener mHttpListener) {
		this.mHttpListener = mHttpListener;
	}
	public abstract int getRequestType();
	public boolean isRepeatRequest(HttpRequest reqeust){
//		if(mObArrary!=null){
//			if(mObArrary.equals(reqeust.getData())){
//				return true;
//			}
//		}
		return false;
	}
	
	public String getRequestMethod() {
		return mRequestMethod;
	}
	
	public void setRequestMethod(String reqeustMethod){
		this.mRequestMethod = reqeustMethod;
	}
	/**
	 * 
	 * @param handler
	 */
	public void setHandler(Handler handler){
		this.mHandler = handler;
	}
	
	protected void addSameRequest(HttpRequest sameRequest){
		mSameRequestList.add(sameRequest);
	}
	
	protected List<HttpRequest> getSameRequestList(){
		return mSameRequestList;
	}
	
}
